package item.lxp.controller;


import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;

@RestController("/file")
public class FileController {

    @PostMapping
    public String fileUpload(MultipartFile file){
        System.out.println(file.getName());
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getSize());
        String path = "";
        File f = new File(path,new Date().getTime() +".txt");
//        try {
//            InputStream inputStream = file.getInputStream();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return f.getAbsolutePath();  // File的绝对路径
    }

    @GetMapping
    public String fileDownload(HttpServletRequest request, HttpServletResponse responce) throws Exception{
        try(OutputStream out = responce.getOutputStream();
            InputStream input = new FileInputStream("");
        ){
            responce.setContentType("application/x-download");
            responce.setHeader("Context-Disposition","attachment;filename=test.txt");
            IOUtils.copy(input,out);
        }catch(Exception e){
            e.printStackTrace();
        }

        return "";
    }


}
